/*
 * Copyright 2009 (c) Patrick Pietens, braemarframework.org.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package indigo.loader.loaders
{
	import indigo.utils.garbage;
	import indigo.loader.errors.LoadError;
	import indigo.loader.types.LoadingStates;

	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	/**
	 * The LoaderTxt class can be used directly to load single text files.
	 *  
	 * <p>Although it is recommend to use the <i>loadText()</i> function. The <i>loadText()</i> 
	 * function will not only provide an easy way to load files. It also stores loaded content for
	 * later use in your application. Stored content can be accessed everywhere in your application 
	 * by using the <i>getContent()</i> function.</p>
	 *
	 */
	public class TextLoader extends AbstractLoader
	{
		/**
		 * String that indicates the dataFormat of tyhe URLLoader
		 */
		protected var _dataFormat:String;

		
		/**
		 * Constructor
		 * 
		 */  
		public function TextLoader()
		{ 
			super( );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		override public function load( filename:String ):void
		{
			super.load( filename );

			// Set properties
			this._dataFormat = URLLoaderDataFormat.TEXT;
			
			// Create loader
			this._loader = new URLLoader( );
			this.loader.dataFormat = this._dataFormat;
			
			// Add lsisteners			
			this.loader.addEventListener( ProgressEvent.PROGRESS, this.onProgress );
			this.loader.addEventListener( Event.COMPLETE, this.onComplete );
			this.loader.addEventListener( IOErrorEvent.IO_ERROR, this.onError );
			this.loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, this.onError );
			
			// Load file
			var myRequest:URLRequest = new URLRequest( this.filename );
			this.loader.load( myRequest );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */		
		override protected function onProgress( event:ProgressEvent ):void
		{
			this._content = this.loader.data;	
			super.onProgress( event );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		override protected function onComplete( event:Event ):void
		{
			this._content = this.loader.data;
			super.onComplete( event );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		override public function set context( value:* ):void
		{
			if ( value is LoaderContext )
				this._context = value;
				
			else
				throw new LoadError( LoadError.ARGUMENT_EXCEPTION );				
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		override public function stop():void
		{
			super.stop( );
			
			if ( this.loadingState == LoadingStates.LOADING )
				this.loader.close( );
		}

		
		/**
		 * Reference to the actual loader
		 * 
		 */
		public function get loader():URLLoader
		{
			return this._loader as URLLoader;
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		override public function dispose( event:Event = null ):void
		{
			log( "Disposing Loader: " + this.filename );
			
			if ( this.loadingState != LoadingStates.NOT_LOADED )
			{
				// Remove listeners
				this.loader.removeEventListener( ProgressEvent.PROGRESS, this.onProgress );
				this.loader.removeEventListener( Event.COMPLETE, this.onComplete );
				this.loader.removeEventListener( IOErrorEvent.IO_ERROR, this.onError );
				this.loader.removeEventListener( SecurityErrorEvent.SECURITY_ERROR, this.onError );
			}
						
			super.dispose( event );
			
			// Nullify properties
			this._dataFormat = null;
			this._loader = null;
		}		
	}
}